package org.demo.baoleme;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

import org.demo.baoleme.common.SimpleLogger;

@SpringBootApplication
@MapperScan("org.demo.baoleme.mapper")
@EnableWebSocket
public class BaolemeApplication  implements WebMvcConfigurer {

    public static void main(String[] args) {
        SimpleLogger.info("后端服务启动中...");
        try {
            SpringApplication.run(BaolemeApplication.class, args);
            SimpleLogger.info("后端服务启动成功");
        } catch (Exception e) {
            // 优先判断异常类型（更精准）
            if (e instanceof org.springframework.jdbc.CannotGetJdbcConnectionException 
                || (e.getCause() instanceof java.sql.SQLException)) {
                // 数据库连接相关异常
                SimpleLogger.error("数据库连接错误: " + e.getMessage()); // 记录完整堆栈
            } else {
                // 其他启动异常
                SimpleLogger.error("启动异常: " + e.getMessage());
            }
            throw e; // 保持应用终止
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }
}
